
public class ContigAndGeneReader
{
  /*
  Contig and Gene Reader class will use these column values
  to locate elements in the file 
  */
  int idColumn = 0; //eg. Bt_EG10144i_contig00236_G1.1.pep
  int contigColumn = 1;//eg. 1 to 96
  int startColumn = 2;//nucleotide (from 0) at which gene starts
  int stopColumn = 3;//nucleotide (from 0) at which gene ends
  int knownnessColumn = 4;//either KNOWN or PUTATIVE
  int toxinColumn = 5;//name of toxin, or NA
  
  DataTable dataTable;

  /*
  CONSTRUCTOR
  loads file and parses into 2D data array
  nothing in this constructor is contig specific
  */
  ContigAndGeneReader( String filename )
  {
    println( "opening file and reading data" );
    
    dataTable = new DataTable( filename );

  }
  
  //--------------------------------------GETTERS-------------------------------------
  /*
  Method: getNumberOfContigs
  Returns: numberOfContigs
  */
  int getNumberOfContigs()
  {
     //NOTE:  start by showing 10 
     return dataTable.getMaxIntInColumn( contigColumn ); 
     //return 10;
  }
  
  
  Contig getContig( int i )
  {
    if( i >= dataTable.getMaxIntInColumn( contigColumn ))//TO DO:  make sure it is >= not just >
    {
      println( "error: requested contig exeeds maximum size" );
      return null;
    }
    
    //get range of rows in which contig can be found
    int startRow = dataTable.getFirstRowOfIntElt( contigColumn, i+1 ); //assume indexing 0 - n-1
    int endRow = dataTable.getLastRowOfIntElt( contigColumn, i+1 );
    
    if( startRow == -1 || endRow == -1 ) //make sure range of rows is valid
    {
      println( "error: requested contig not matched in table" );
      return null;
    }
    
    //Create and add Genes to contig
    String unprocessedContigName = dataTable.getStringElement( startRow, idColumn );//get raw name
    String[] contigIds = processContigName( unprocessedContigName );//process name into pieces
    
    //count the genes for this contig
    int geneCount = 0;
    for(int row = startRow; row <= endRow; row++)//loop through all rows in which this contig is possibly present
    {
      if( dataTable.getIntElement( row, contigColumn ) == (i+1) ) //make sure it matches contig number
      {
        geneCount++;
      }  
    }
    
    int maxNT = dataTable.getMaxIntInColumn( stopColumn );//default start max nt value
    Contig c = new Contig( contigIds[0], contigIds[1], contigIds[2], geneCount, maxNT );
    
    //then add gene
    for(int row = startRow; row <= endRow; row++)//loop through all rows in which this contig is possibly present
    {
      if( dataTable.getIntElement( row, contigColumn ) == (i+1) ) //make sure it matches contig number
      {
        int start = dataTable.getIntElement( row, startColumn );
        int end = dataTable.getIntElement( row, stopColumn );
        String known = dataTable.getStringElement( row, knownnessColumn );
        String toxinName = dataTable.getStringElement( row, toxinColumn );
       
        c.addGene( start, end, known, toxinName );
      }  
    }
    
    return c;
  }
  
  //------------------------------------END GETTERS-----------------------------------
  
  //------------------------------------DATA PROCESSING METHODS-----------------------
  String[] processContigName( String unprocName )
  {     
    String[] temp = new String[3];
    StringTokenizer st = new StringTokenizer( unprocName , "_" );
    temp[0] = st.nextToken(); //species name
    temp[1] = st.nextToken(); //string name
    temp[2] = st.nextToken();//contig name 
    
    return temp;
  }
  
  //--------------------------------END DATA PROCESSING METHODS-----------------------
  //-----------------------------------UTILITY METHODS--------------------------------
  /*
  Method: toString
  Action: converts object to string
  Parameters: none
  Returns: string value of object
  Note: println method calls this
  */
  String toString()
  {
    return dataTable.toString();
  }
  //----------------------------------END UTILITY METHODS-----------------------------

}

